package com.zhonghan.recharge.service.impl.jisu;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.zhonghan.common.bean.Result;
import com.zhonghan.common.utils.JiSuOrder;
import com.zhonghan.common.utils.StringUtils;

//import net.sf.json.JSONObject;

public class JiSuRecharge {
	
	private static final Logger logger = LogManager.getLogger(JiSuRecharge.class);
	//public static final String APPKEY = "1ebd266b17052bd4";// 你的appkey
    public static final String URL = "http://api.jisuapi.com/fuelcardrecharge/recharge";
    public static final String DETAIL_URL ="http://api.jisuapi.com/fuelcardrecharge/orderdetail";
    //public static final String fuelcard = "1000110000001234567";// 要充值的加油卡卡号
  //public static final String amount = "";// 充值的金额 单位：元
   // public static final String outorderno = "";// 外部订单号
   // public static final String appsecret = "Hpah5CexfPJkWrHJ4hW5JqSBKsYUcv1S";
    public static Map<String, String> queryarr;
    
    /**
     * 
     * @param myorderId  外部订单
     * @param gasnumber  油卡卡号
     * @param gasname    油卡名称
     * @param gasphone   油卡电话
     * @param recardno   充值卡卡号，暂时不用
     * myAmount   充值金额
     */
    /*public static Result toRecharge(String myorderId,String gasnumber,String gasname, String gasphone,Integer myAmount) {
    	try {
    		Result result = new Result();
    		queryarr = new HashMap<String, String>();
            queryarr.put("amount", String.valueOf(myAmount));
            queryarr.put("fuelcard", gasnumber);
            queryarr.put("outorderno", myorderId);
            String sign = makeSign(queryarr, appsecret);// md5(fuelcard,amount,outorderno按key排序)
            System.out.println("sign="+sign);
            String jiSuResult = null;
            String url = URL + "?appkey=" + APPKEY + "&fuelcard=" + gasnumber + "&amount=" + String.valueOf(myAmount)
                    + "&outorderno=" + myorderId + "&sign=" + sign;
            
            //请求充值
            jiSuResult = HttpUtil.sendGet(url, "utf-8");
            logger.info(jiSuResult);
            JSONObject json = JSONObject.parseObject(jiSuResult);
            if (json.getInteger("status") != 0) {
            	logger.info(json.getString("msg"));
                result.setCode("0");
            } else {
                JSONObject resultarr = JSONObject.parseObject(json.getString("result"));
                String fuelcard = resultarr.getString("fuelcard");
                String totalfee = resultarr.getString("totalfee");
                String amount = resultarr.getString("amount");
                String outorderno = resultarr.getString("outorderno");
                String orderno = resultarr.getString("orderno");
                String rechargestatus = resultarr.getString("rechargestatus");
                logger.info(fuelcard + " " + " " + amount + " " + outorderno + " " + orderno + " "
                        + totalfee + " " + rechargestatus);
                JiSuOrder order = new JiSuOrder();
                order.setFuelcard(fuelcard);
                order.setTotalFee(totalfee);
                order.setAmount(amount);
                order.setOutorderno(outorderno);
                order.setOrderno(orderno);
                order.setRechargestatus(new Integer(rechargestatus));
                
                result.setCode("1");//极速订单成功
                result.setData(order);
            }
            return result;
    		
    	}catch(Exception e) {
    		logger.info("调用极速接口，充值失败.......");
    	}
    	return null;
    }*/
    /**
     * 查看极速订单状态
     * @param orderno
     * @param outorderno
     * @return
     */
    /*public static JiSuOrder getJiSuOrderDetail(String orderno,String outorderno) {
        String result = null;
        String url = DETAIL_URL + "?appkey=" + APPKEY + "&orderno=" + orderno + "&outorderno=" + outorderno;
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            logger.info("查询加油订单："+result);
            JSONObject json = JSONObject.parseObject(result);
            if (json.getInteger("status") != 0) {
                logger.info(json.getString("msg"));
            } else {
            	JSONObject resultarr = JSONObject.parseObject(json.getString("result"));
                String orderno1 = resultarr.getString("orderno");
                String outorderno1 = resultarr.getString("outorderno");
                String account = resultarr.getString("account");
                String num = resultarr.getString("num");
                String status = resultarr.getString("status");
                String rechargestatus = resultarr.getString("rechargestatus");
                String amount = resultarr.getString("amount");
                String totalfee = resultarr.getString("totalfee");
                String addtime = resultarr.getString("addtime");
                String type = resultarr.getString("type");
                logger.info(type + " " + orderno + " " + outorderno + " " + account + " " + num + " " + status
                        + " " + rechargestatus + " " + amount + " " + totalfee + " " + addtime);
                
                JiSuOrder order = new JiSuOrder(); 
                order.setTotalFee(totalfee);
                order.setAmount(amount);
                order.setOutorderno(outorderno1);   
                order.setOrderno(orderno1);
                order.setRechargestatus(new Integer(rechargestatus));   //jisu  订单状态
                return order;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }*/
    
 
    public static void Get() throws NoSuchAlgorithmException, UnsupportedEncodingException {
        
 
        try {
        	/*queryarr = new HashMap<String, String>();
            queryarr.put("amount", amount);
            queryarr.put("fuelcard", fuelcard);
            queryarr.put("outorderno", outorderno);
            String sign = makeSign(queryarr, appsecret);// md5(fuelcard,amount,outorderno按key排序)
            String result = null;
            String url = URL + "?appkey=" + APPKEY + "&fuelcard=" + fuelcard + "&amount=" + amount
                    + "&outorderno=" + outorderno + "&sign=" + sign;
            result = HttpUtil.sendGet(url, "utf-8");
            System.out.println(result);*/
            /*JSONObject json = JSONPObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultarr = (JSONObject) json.opt("result");
                String fuelcard = resultarr.getString("fuelcard");
                String totalfee = resultarr.getString("totalfee");
                String amount = resultarr.getString("amount");
                String outorderno = resultarr.getString("outorderno");
                String orderno = resultarr.getString("orderno");
                String rechargestatus = resultarr.getString("rechargestatus");
                System.out.println(fuelcard + " " + amount + " " + outorderno + " " + orderno + " "
                        + totalfee + " " + rechargestatus);
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static String makeSign(Map<String, String> queryarr, String appsecret)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        TreeMap map = new TreeMap(queryarr);
        Iterator ir = map.keySet().iterator();
        String str = new String();
        while (ir.hasNext()) {
            Object key = ir.next();
            str += map.get(key);
        }
        str += appsecret;
        return getMd5(str.getBytes());
    }
 
    public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException {
        String s = null;
        char hexDigist[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(buffer);
        byte[] datas = md.digest(); // 16个字节的长整数
        char[] str = new char[2 * 16];
        int k = 0;
        for (int i = 0; i < 16; i++) {
            byte b = datas[i];
            str[k++] = hexDigist[b >>> 4 & 0xf];// 高4位
            str[k++] = hexDigist[b & 0xf];// 低4位
        }
        s = new String(str);
        return s;
    }
    
    
    public static Result toRechargeII(String myorderId,String gasnumber,String gasname, String gasphone,Integer myAmount,String appkey,String appsecret) {
    	try {
    		Result result = new Result();
    		queryarr = new HashMap<String, String>();
            queryarr.put("amount", String.valueOf(myAmount));
            queryarr.put("fuelcard", gasnumber);
            queryarr.put("outorderno", myorderId);
            String sign = makeSign(queryarr, appsecret);// md5(fuelcard,amount,outorderno按key排序)
            System.out.println("sign="+sign);
            String jiSuResult = null;
            String url = URL + "?appkey=" + appkey + "&fuelcard=" + gasnumber + "&amount=" + String.valueOf(myAmount)
                    + "&outorderno=" + myorderId + "&sign=" + sign;
            
            //请求充值
            jiSuResult = HttpUtil.sendGet(url, "utf-8");
            logger.info(jiSuResult);
            JSONObject json = JSONObject.parseObject(jiSuResult);
            if (json.getInteger("status") != 0) {
            	logger.info(json.getString("msg"));
                result.setCode("0");
            } else {
                JSONObject resultarr = JSONObject.parseObject(json.getString("result"));
                String fuelcard = resultarr.getString("fuelcard");
                String totalfee = resultarr.getString("totalfee");
                String amount = resultarr.getString("amount");
                String outorderno = resultarr.getString("outorderno");
                String orderno = resultarr.getString("orderno");
                String rechargestatus = resultarr.getString("rechargestatus");
                logger.info(fuelcard + " " + " " + amount + " " + outorderno + " " + orderno + " "
                        + totalfee + " " + rechargestatus);
                JiSuOrder order = new JiSuOrder();
                order.setFuelcard(fuelcard);
                order.setTotalFee(totalfee);
                order.setAmount(amount);
                order.setOutorderno(outorderno);
                order.setOrderno(orderno);
                order.setRechargestatus(new Integer(rechargestatus));
                
                result.setCode("1");//极速订单成功
                result.setData(order);
            }
            return result;
    		
    	}catch(Exception e) {
    		logger.info("调用极速接口，充值失败.......");
    	}
    	return null;
    }
    /**
     * 查看极速订单状态
     * @param orderno
     * @param outorderno
     * @return
     */
    public static JiSuOrder getJiSuOrderDetailII(String orderno,String outorderno,String appkey) {
        String result = null;
        String url = DETAIL_URL + "?appkey=" + appkey + "&orderno=" + orderno + "&outorderno=" + outorderno;
        try {
            result = HttpUtil.sendGet(url, "utf-8");
            logger.info("查询加油订单："+result);
            JSONObject json = JSONObject.parseObject(result);
            if (json.getInteger("status") != 0) {
                logger.info(json.getString("msg"));
            } else {
            	if(StringUtils.isNotEmpty(json.getString("result"))){
            		JSONObject resultarr = JSONObject.parseObject(json.getString("result"));
            		String orderno1 = resultarr.getString("orderno");
            		String outorderno1 = resultarr.getString("outorderno");
            		String account = resultarr.getString("account");
            		String num = resultarr.getString("num");
            		String status = resultarr.getString("status");
            		String rechargestatus = resultarr.getString("rechargestatus");
            		String amount = resultarr.getString("amount");
            		String totalfee = resultarr.getString("totalfee");
            		String addtime = resultarr.getString("addtime");
            		String type = resultarr.getString("type");
            		logger.info(type + " " + orderno + " " + outorderno + " " + account + " " + num + " " + status
            				+ " " + rechargestatus + " " + amount + " " + totalfee + " " + addtime);
            		
            		JiSuOrder order = new JiSuOrder(); 
            		order.setTotalFee(totalfee);
            		order.setAmount(amount);
            		order.setOutorderno(outorderno1);   
            		order.setOrderno(orderno1);
            		order.setRechargestatus(new Integer(rechargestatus));   //jisu  订单状态
            		return order;
            	}
            	
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
